@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

:: ====================================
::           脚本配置
:: ====================================
set "LOCAL_VERSION=1.0"
set "VERSION_URL=https://gitee.com/huaming007/win11--open-script-hot-update/raw/master/version.txt"

:: 状态文本映射
set "STATUS_TEXT_0=未安装"
set "STATUS_TEXT_1=已安装 (跳过)"
set "STATUS_TEXT_3=安装成功"
set "STATUS_TEXT_4=安装失败"
set "STATUS_TEXT_UNKNOWN=状态未知"

:: 软件列表定义
set "SOFTWARE_COUNT=5"
set "SOFTWARE_1_NAME=微信"
set "SOFTWARE_1_STATUS_VAR=WECHAT_STATUS"
set "SOFTWARE_1_SINGLE_GOTO=install_wechat_single"
set "SOFTWARE_1_CORE_GOTO=install_wechat_core"

set "SOFTWARE_2_NAME=QQ NT"
set "SOFTWARE_2_STATUS_VAR=QQ_STATUS"
set "SOFTWARE_2_SINGLE_GOTO=install_qq_single"
set "SOFTWARE_2_CORE_GOTO=install_qq_core"

set "SOFTWARE_3_NAME=WPS Office"
set "SOFTWARE_3_STATUS_VAR=WPS_STATUS"
set "SOFTWARE_3_SINGLE_GOTO=install_wps_single"
set "SOFTWARE_3_CORE_GOTO=install_wps_core"

set "SOFTWARE_4_NAME=360极速浏览器X"
set "SOFTWARE_4_STATUS_VAR=CHROME_STATUS"
set "SOFTWARE_4_SINGLE_GOTO=install_360chrome_single"
set "SOFTWARE_4_CORE_GOTO=install_360chrome_core"

set "SOFTWARE_5_NAME=火绒安全"
set "SOFTWARE_5_STATUS_VAR=HUORONG_STATUS"
set "SOFTWARE_5_SINGLE_GOTO=install_huorong_single"
set "SOFTWARE_5_CORE_GOTO=install_huorong_core"

color 85
goto :check_network


:: =================================================================
::            1. 检查网络连接
:: =================================================================
:check_network
cls
echo ====================================
    echo      软件安装脚本 By·花名
    echo ====================================
    echo.
    echo 正在检查网络连接...
    ping baidu.com -n 1 > nul
    if !errorlevel! neq 0 (
        echo 错误：网络连接失败，5秒后重试...
        timeout /t 5 > nul
        goto :check_network
    )
    echo 网络连接成功！
    timeout /t 1 > nul
    goto :check_for_updates


:: =================================================================
::            2. 检查脚本更新 (60秒超时) - 由 PowerShell 处理
:: =================================================================
:check_for_updates
cls
echo ====================================
echo          正在检查脚本更新...
echo ====================================
echo.

:: 检查 PowerShell 是否可用
where powershell.exe >nul 2>&1
if !errorlevel! neq 0 (
    echo 错误：未找到 PowerShell.exe，无法执行更新检查。
    echo 请确保您的系统安装了 PowerShell。
    timeout /t 5 > nul
    goto :main_script
)

:: 检查 PowerShell 脚本是否存在
if not exist "%TEMP%\updater.ps1" (
    echo 错误：更新脚本 (updater.ps1) 不存在于 %TEMP% 目录。
    echo 请确保 updater.ps1 已正确创建。
    timeout /t 5 > nul
    goto :main_script
)

:: 调用 PowerShell 脚本进行更新检查
:: PowerShell 脚本返回 1 表示已更新并重启，当前批处理脚本应退出
:: 返回 0 表示未更新或更新失败，当前批处理脚本继续执行
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP%\updater.ps1" "%LOCAL_VERSION%" "%VERSION_URL%" "%~f0"
if !errorlevel! equ 1 (
    exit /b
)

:: 如果 PowerShell 脚本没有重启，则继续主逻辑
goto :main_script


:: =================================================================
::            3. 主脚本逻辑
:: =================================================================
:main_script
set "DOWNLOAD_DIR=%TEMP%\SoftwareInstallers"

set "WECHAT_STATUS=0"
set "QQ_STATUS=0"
set "WPS_STATUS=0"
set "CHROME_STATUS=0"
set "HUORONG_STATUS=0"

:menu
cls
color 85
echo ====================================
echo          请选择要安装的软件
echo ====================================
echo.

for /l %%i in (1,1,!SOFTWARE_COUNT!) do (
    call :get_status_text !SOFTWARE_LIST_%%i_STATUS_VAR! CURRENT_STATUS_TEXT
    echo %%i. 安装 !SOFTWARE_LIST_%%i_NAME! (!CURRENT_STATUS_TEXT!)
)
echo.
echo A. 安装全部软件 (推荐)
echo B. 清理安装缓存
echo C. 退出
echo.
set /p "choice=请输入您的选择 (例如: 1 或 A): "

if /i "!choice!" geq "1" if /i "!choice!" leq "!SOFTWARE_COUNT!" (
    set "GOTO_LABEL=!SOFTWARE_LIST_!choice!_SINGLE_GOTO!"
    goto !GOTO_LABEL!
)
if /i "!choice!"=="A" goto :install_all
if /i "!choice!"=="B" goto :cleanup
if /i "!choice!"=="C" goto :exit_script

echo 输入无效，请重新输入。
timeout /t 2 > nul
goto :menu
:install_all
echo 正在安装所有软件...
for /l %%i in (1,1,!SOFTWARE_COUNT!) do (
    set "CORE_GOTO=!SOFTWARE_LIST_%%i_CORE_GOTO!"
    call :!CORE_GOTO!
)
echo.
echo ====================================
echo          所有任务已处理完毕
echo ====================================
echo.
echo 最终安装状态:
call :show_final_status
echo.
echo 按任意键返回主菜单。
pause > nul
goto :menu
:: ==============================================================
::                   状态显示函数
:: ==============================================================
:get_status_text
set "VAR_NAME=%~1"
set "OUT_VAR_NAME=%~2"
set "STATUS_VALUE=!%VAR_NAME%!"
set "LOOKUP_VAR=STATUS_TEXT_!STATUS_VALUE!"
if defined !LOOKUP_VAR! (
    set "%OUT_VAR_NAME%=!%LOOKUP_VAR%!"
) else (
    set "%OUT_VAR_NAME%=!STATUS_TEXT_UNKNOWN!"
)
goto :eof
:show_final_status
for /l %%i in (1,1,!SOFTWARE_COUNT!) do (
    call :get_status_text !SOFTWARE_LIST_%%i_STATUS_VAR! CURRENT_STATUS_TEXT
    echo   - !SOFTWARE_LIST_%%i_NAME!: !CURRENT_STATUS_TEXT!
)
goto :eof

:: ==============================================================
::                   通用下载与安装函数
:: ==============================================================
:download_and_install
set "SOFTWARE_NAME=%~1"
set "DOWNLOAD_URL=%~2"
set "FILENAME=%~3"
set "STATUS_VAR_NAME=%~4"

:: 确保临时目录存在并进入
if not exist "%DOWNLOAD_DIR%" mkdir "%DOWNLOAD_DIR%"
pushd "%DOWNLOAD_DIR%"

set "DOWNLOAD_PATH=%CD%\!FILENAME!"
echo.
echo ===== 正在下载 %SOFTWARE_NAME% =====
echo 下载链接: !DOWNLOAD_URL!
echo 保存至: !DOWNLOAD_PATH!
:: 使用 aria2c 进行多线程下载
aria2c -c -x 10 -s 10 -d "%CD%" -o "!FILENAME!" "!DOWNLOAD_URL!"
if !errorlevel! neq 0 (
    echo 错误：下载 %SOFTWARE_NAME% 失败。错误代码: !errorlevel!。
    echo 请检查网络、直链地址或 aria2c.exe 是否存在。
    set "!STATUS_VAR_NAME!=4"
    popd
    goto :eof
)
echo.
echo %SOFTWARE_NAME% 下载完成。
echo.
echo ===== 正在启动 %SOFTWARE_NAME% 安装程序，请在弹出的界面中完成安装。 =====
echo 安装程序路径: "!DOWNLOAD_PATH!"
echo.

:: 检查文件是否存在
if not exist "!DOWNLOAD_PATH!" (
    echo 错误：未找到下载的安装程序。
    echo 路径: "!DOWNLOAD_PATH!"
    set "!STATUS_VAR_NAME!=4"
    popd
    goto :eof
)

start /wait "" "!DOWNLOAD_PATH!"

:: 用户确认安装结果
set /p "INSTALL_CONFIRM=请确认 %SOFTWARE_NAME% 是否安装成功？(Y/N): "
if /i "!INSTALL_CONFIRM!"=="Y" (
    echo %SOFTWARE_NAME% 安装成功。
    set "!STATUS_VAR_NAME!=3"
) else (
    echo %SOFTWARE_NAME% 安装被用户取消或失败。
    set "!STATUS_VAR_NAME!=4"
)

popd
goto :eof

:: ==============================================================
::                   各个软件的安装逻辑
:: ==============================================================

:install_wechat_core
    echo.
    echo ===== 正在检查 微信... =====
    reg query "HKEY_CURRENT_USER\Software\Tencent\WeChat" /v InstallPath > nul 2>&1
    if !errorlevel! equ 0 (
        echo 微信 已安装，跳过。
        set "WECHAT_STATUS=1"
    ) else (
        echo 微信 未安装，开始下载并安装...
        call :download_and_install "微信" "https://dldir1v6.qq.com/weixin/Universal/Windows/WeChatWin.exe" "WeChatSetup.exe" "WECHAT_STATUS"
    )
    goto :eof

:install_qq_core
    echo.
    echo ===== 正在检查 QQ NT... =====
    reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{14B3B8B8-5A24-43A3-888A-2A931A8B426D}" > nul 2>&1
    if !errorlevel! equ 0 (
        echo QQ NT 已安装，跳过。
        set "QQ_STATUS=1"
    ) else (
        echo QQ NT 未安装，开始下载并安装...
        call :download_and_install "QQ NT" "https://dldir1v6.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.21_250904_x64_01.exe" "QQNTSetup.exe" "QQ_STATUS"
    )
    goto :eof

:install_wps_core
    echo.
    echo ===== 正在检查 WPS Office... =====
    reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WPS Office" > nul 2>&1
    if !errorlevel! equ 0 (
        echo WPS Office 已安装，跳过。
        set "WPS_STATUS=1"
    ) else (
        echo WPS Office 未安装，开始下载并安装...
        call :download_and_install "WPS Office" "https://official-package.wpscdn.cn/wps/download/WPS_Setup_X64_22525.exe" "WPSSetup.exe" "WPS_STATUS"
    )
    goto :eof

:install_360chrome_core
    echo.
    echo ===== 正在检查 360极速浏览器X... =====
    if exist "%ProgramFiles(x86)%\360Chrome\Chrome\Application\360chrome.exe" (
        echo 360极速浏览器X 已安装，跳过。
        set "CHROME_STATUS=1"
    ) else (
        echo 360极速浏览器X 未安装，开始下载并安装...
        call :download_and_install "360极速浏览器X" "https://down.360safe.com/cse/360csex_setup.exe" "360csex_setup.exe" "CHROME_STATUS"
    )
    goto :eof

:install_huorong_core
    echo.
    echo ===== 正在检查 火绒安全... =====
    if exist "%ProgramFiles%\Huorong\Sysdiag\HipsMain.exe" (
        echo 火绒安全 已安装，跳过。
        set "HUORONG_STATUS=1"
    ) else (
        echo 火绒安全 未安装，开始下载并安装...
        call :download_and_install "火绒安全" "https://down-tencent.huorong.cn/sysdiag-all-x64-6.0.7.8-2025.09.06.1.exe" "HuorongSetup.exe" "HUORONG_STATUS"
    )
    goto :eof

:: ==============================================================
::                   单项安装包装器
:: ==============================================================

:install_wechat_single
    call :install_wechat_core
    goto :menu

:install_qq_single
    call :install_qq_core
    goto :menu

:install_wps_single
    call :install_wps_core
    goto :menu

:install_360chrome_single
    call :install_360chrome_core
    goto :menu

:install_huorong_single
    call :install_huorong_core
    goto :menu

:: ==============================================================
::                   清理
:: ==============================================================
:cleanup
    echo.
    echo ====================================
    echo          正在执行清理...
    echo ====================================
    echo.
    echo 正在删除已下载的安装包...
    if exist "%DOWNLOAD_DIR%" (
        rd /s /q "%DOWNLOAD_DIR%"
        echo 清理完成。
    ) else (
        echo 下载目录不存在，无需清理。
    )
    timeout /t 2 > nul
    goto :menu

:: ==============================================================
::                   退出
:: ==============================================================
:exit_script
exit /b